package com.rr.sc.security;

import java.security.MessageDigest;

import org.apache.commons.codec.binary.Hex;
import org.springframework.security.core.context.SecurityContextHolder;

import com.rr.sc.model.User;

public class SecurityUtils {

    public static User getLoggedUser() {
	if (SecurityContextHolder.getContext().getAuthentication() != null) {
	    MyUserDetails userDetails = (MyUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
	    if (userDetails != null) {
		return userDetails.getUser();
	    }
	}
	return null;
    }

    public static String getMD5(String value) throws Exception {
	byte[] bytesOfMessage = value.getBytes("UTF-8");
	MessageDigest md = MessageDigest.getInstance("MD5");
	byte[] theDigest = md.digest(bytesOfMessage);
	String md5 = new String(Hex.encodeHex(theDigest));
	return md5;
    }
}
